package com.banciyuan.bcywebview.biz.write.write;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.Multi;

public class WriteEditAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<Multi> plist;
    private HeaderHolder headerHolder;

    public WriteEditAdapter(Context mContext, List<Multi> plist, HeaderHolder headerHolder) {
        this.mContext = mContext;
        this.plist = plist;
        this.headerHolder = headerHolder;
        mInflater=LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case 0:
                return headerHolder;
            case 1:
                return new ImageHolder(mInflater.inflate(R.layout.post_img_item,null));
            default:
                return new ImageHolder(mInflater.inflate(R.layout.post_img_item,null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)){
            case 1:
                ImageHolder imageHolder= (ImageHolder) holder;
                setImage(position-1,imageHolder);
                break;
        }
    }

    private void setImage(final int position, final ImageHolder imageHolder){
        ImageLoader.getInstance().displayImage(plist.get(position).getPath(), imageHolder.content, ImageOptions.options);
        imageHolder.img_cancel.setVisibility(View.GONE);
    }

    @Override
    public long getItemId(int postion) {
        return postion;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return 0;
        }else{
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        return plist.size()+1;
    }

    private class ImageHolder extends RecyclerView.ViewHolder {
        ImageView content;
        ImageView img_cancel;

        public ImageHolder(View itemView) {
            super(itemView);
            content= (ImageView) itemView.findViewById(R.id.item_group_content);
            img_cancel = (ImageView) itemView.findViewById(R.id.item_group_cancel);
        }
    }

}